import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:uikit/http/dao/login_dao.dart';
import 'package:uikit/theme/riki_base_theme.dart';
import 'package:uikit/uikit.dart';
import 'package:provider/provider.dart';
import 'package:uikit/utils/toast_util.dart';
import '../navigator/bottom_navigator.dart';
import '../navigator/hi_navigator.dart';
import '../pages/login_register/page/login_page.dart';
import '../pages/login_register/page/register_page.dart';
import '../pages/main/video_detail_page.dart';
import '../pages/widgets/color.dart';
import 'global_providers.dart';

class BiliApp extends StatefulWidget {
  const BiliApp({Key? key}) : super(key: key);

  @override
  _BiliAppState createState() => _BiliAppState();
}

class _BiliAppState extends State<BiliApp> {
  final BiliRouteDelegate _routeDelegate = BiliRouteDelegate();

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: providers,
      child: buildRootWidget(),
    );
    return MaterialApp(
      home: Router(routerDelegate: _routeDelegate),
      theme: ThemeData(primarySwatch: white),
    );
  }

  Widget buildRootWidget() {
    return Builder(builder: (context) {
      return RikiUikit.init(
        context: context,
        designSize: const Size(375, 667),
        builder: (BuildContext ctx, RikiBaseTheme theme) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            theme: theme.theme,
            //国际化支持
            localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: const <Locale>[
              Locale('zh', 'CN'),
              Locale('en', 'US'),
            ],
            home: Router(routerDelegate: _routeDelegate),
          );
        },
      );
    });
  }
}

class BiliRouteDelegate extends RouterDelegate<BiliRoutePath> with ChangeNotifier, PopNavigatorRouterDelegateMixin<BiliRoutePath> {
  @override
  GlobalKey<NavigatorState>? get navigatorKey => GlobalKey<NavigatorState>();

  //为Navigator设置一个key，必要的时候可以通过navigatorKey.currentState来获取到NavigatorState对象
  BiliRouteDelegate() {
    //实现路由跳转逻辑
    HiNavigator.getInstance().registerRouteJump(RouteJumpListener(onJumpTo: (RouteStatus routeStatus, {Map? args}) {
      _routeStatus = routeStatus;
      // if (routeStatus == RouteStatus.detail) {
      //   //this.videoModel = args['videoMo'];
      // }
      notifyListeners();
    }));
    //设置网络错误拦截器
    // HiNet.instance().setErrorInterceptor((error) {
    //   if (error is NeedLogin) {
    //     //清空失效的登录令牌
    //     HiCache.getInstance().setString(LoginDao.BOARDING_PASS, null);
    //     //拉起登录
    //     HiNavigator.getInstance().onJumpTo(RouteStatus.login);
    //   }
    // });
  }

  RouteStatus _routeStatus = RouteStatus.home;
  List<MaterialPage> pages = [];
  //VideoMo videoModel;

  @override
  Widget build(BuildContext context) {
    var index = getPageIndex(pages, routeStatus);
    List<MaterialPage> tempPages = pages;
    if (index != -1) {
      //要打开的页面在栈中已存在，则将该页面和它上面的所有页面进行出栈
      //tips 具体规则可以根据需要进行调整，这里要求栈中只允许有一个同样的页面的实例
      tempPages = tempPages.sublist(0, index);
    }
    var page;
    if (routeStatus == RouteStatus.home) {
      //跳转首页时将栈中其它页面进行出栈，因为首页不可回退
      pages.clear();
      page = pageWrap(const BottomNavigator());
    } else if (routeStatus == RouteStatus.detail) {
      page = pageWrap(VideoDetailPage());
    } else if (routeStatus == RouteStatus.registration) {
      page = pageWrap(RegisterPage());
    } else if (routeStatus == RouteStatus.login) {
      page = pageWrap(LoginPage());
    }
    //重新创建一个数组，否则pages因引用没有改变路由不会生效
    tempPages = [...tempPages, page];
    //通知路由发生变化
    HiNavigator.getInstance().notify(tempPages, pages);
    pages = tempPages;
    return WillPopScope(
      //fix Android物理返回键，无法返回上一页问题@https://github.com/flutter/flutter/issues/66349
      onWillPop: () async {
        return navigatorKey!.currentState!.maybePop();
      },
      child: Navigator(
        key: navigatorKey,
        pages: pages,
        onPopPage: (route, result) {
          print("onPopPage onPopPage onPopPage");
          if (route.settings is MaterialPage) {
            //登录页未登录返回拦截
            if ((route.settings as MaterialPage).child is LoginPage) {
              if (!hasLogin) {
                ToastUtil.show("请先登录");
                return false;
              }
            }
          }
          //执行返回操作
          if (!route.didPop(result)) {
            return false;
          }
          var tempPages = [...pages];
          pages.removeLast();
          //通知路由发生变化
          HiNavigator.getInstance().notify(pages, tempPages);
          return true;
        },
      ),
    );
  }

  RouteStatus get routeStatus {
    return _routeStatus;
    if (_routeStatus != RouteStatus.registration && !hasLogin) {
      return _routeStatus = RouteStatus.login;
    } else {
      return _routeStatus;
    }
  }

  bool get hasLogin => LoginDao.getBoardingPass().isNotEmpty;

  @override
  Future<void> setNewRoutePath(BiliRoutePath configuration) async {}
}

///定义路由数据，path
class BiliRoutePath {
  final String location;

  BiliRoutePath.home() : location = "/";

  BiliRoutePath.detail() : location = "/detail";
}
